/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycity.servlets;

import com.mycity.dao.PostTab;
import com.mycity.dao.UserTab;
import com.mycity.jsonops.PostOps;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author celikeins
 */
public class Search extends Base {

    public Search(HttpServletRequest request, HttpServletResponse response) {
        super(request, response);
    }

    public void byTag() throws IOException, ServletException {

        response.setContentType("text/html");
        String tag = (String) request.getParameter("searchtag");
        PostTab[] posts = PostOps.searchPostsByTag(tag);
        request.setAttribute("results", posts);

        request.getRequestDispatcher("/searchResults.jsp").include(request, response);
    }

    public void byLocation() throws IOException, ServletException {

        response.setContentType("text/html");
        String id = (String) request.getParameter("loc_id");
        PostTab[] posts = PostOps.searchPostsByLoc(Long.valueOf(id));
        request.setAttribute("results", posts);

        request.getRequestDispatcher("/searchResults.jsp").include(request, response);
    }

    public void nearby() throws IOException, ServletException {

        response.setContentType("text/html");
        String id = (String) request.getParameter("loc_id");
        PostTab[] posts = PostOps.searchPostsNearby(Long.valueOf(id));
        request.setAttribute("results", posts);

        request.getRequestDispatcher("/searchResults.jsp").include(request, response);
    }

    public void recommend() throws IOException, ServletException {
        UserTab user = getCurrentUser();
        if (user == null) {
            response.sendRedirect("/MyCityWeb/login.jsp");
            return;
        }
        response.setContentType("text/html");
        PostTab[] posts = PostOps.recommend(user.getUserId());
        request.setAttribute("results", posts);

        request.getRequestDispatcher("/recommendedPosts.jsp").include(request, response);
    }
}
